package com.codegzy.controller;

import com.codegzy.entity.Emp;
import com.codegzy.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    @RequestMapping("findAll")
    public String findAll(HttpServletRequest request){
        List<Emp> emps = empService.findAll();
        request.setAttribute("emps",emps);
        return "emplist";
    }

    @RequestMapping("save")
    public String save(Emp emp){
        if (emp != null){
            empService.save(emp);
            return "redirect:/emp/findAll";
        }else {
            return "redirect:/addEmp.jsp";
        }
    }

    @RequestMapping("delete")
    public String delete(int id){
        empService.delete(id);
        return "redirect:/emp/findAll";
    }

    @RequestMapping("update")
    public String update(Emp emp,HttpServletRequest request){
        request.getSession().setAttribute("emp",emp);
        return "redirect:/updateEmp.jsp";
    }

    @RequestMapping("updateEmp")
    public String updateEmp(Emp emp){
        empService.updateEmp(emp);
        return "redirect:/emp/findAll";
    }
}
